package com.example.storage;

import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class Foodmanager extends AppCompatActivity {

    private Fooddatabase foodhelper;
    private Cursor cursor;
    private List<Fooditem> foodList = new ArrayList<>();
    private FoodAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_foodmanager);

        foodhelper = new Fooddatabase(this, "FOODSTORE.db", null, 3);//创建数据

        EditText searchFood = findViewById(R.id.search_food);
        RecyclerView recyclerView = findViewById(R.id.recycler_food);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new FoodAdapter(foodList);
        recyclerView.setAdapter(adapter);

        initFood(); // 初始化显示全部食物

        searchFood.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                searchFood(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    private void initFood() {
        foodList.clear();
        cursor = foodhelper.getSortedData();
        startManagingCursor(cursor);
        while (cursor.moveToNext()) {
            Fooditem fooditem = new Fooditem(
                    cursor.getString(cursor.getColumnIndexOrThrow("foodname")),
                    cursor.getString(cursor.getColumnIndexOrThrow("img")),
                    cursor.getInt(cursor.getColumnIndexOrThrow("calories")),
                    cursor.getString(cursor.getColumnIndexOrThrow("unit")));
            foodList.add(fooditem);
        }
        adapter.notifyDataSetChanged();
    }

    private void searchFood(String query) {
        foodList.clear();
        Cursor cursor;
        if (query.isEmpty()) {
            cursor = foodhelper.getSortedData(); // 显示全部食物
        } else {
            cursor = foodhelper.getReadableDatabase().rawQuery(
                    "SELECT * FROM food WHERE foodname LIKE ? OR pinyin LIKE ?",
                    new String[]{"%" + query + "%", "%" + query + "%"});// %query% 通配符，表示表示包含 query 的任何字符串。
        }

        if (cursor != null && cursor.moveToFirst()) {
            do {
                Fooditem fooditem = new Fooditem(
                        cursor.getString(cursor.getColumnIndexOrThrow("foodname")),
                        cursor.getString(cursor.getColumnIndexOrThrow("img")),
                        cursor.getInt(cursor.getColumnIndexOrThrow("calories")),
                        cursor.getString(cursor.getColumnIndexOrThrow("unit")));
                foodList.add(fooditem);
            } while (cursor.moveToNext());
            cursor.close();
        }
        adapter.notifyDataSetChanged();
    }
}
